package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.ListenRecordAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ExpertRecorderDetailModel;
import com.banshuibao.bsb_android.model.RelateDetailModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

public class ExpertRecorderDataileActivity extends BaseActivity implements View.OnClickListener {

    private List<ExpertRecorderDetailModel.DataBean.ExpertVoicesBean> expertVoices;
    private RecyclerView recyclerView;
    private ListView listView;
    private String uuid, recorder_title, recorder_content, recorder_price, parameter_type, name;
    private TextView title, content, price, type;
    private TextView additional;
    private ListenRecordAdapter listenRecordAdapter;
    private TextView tax_tv,trade_tv,topic_tv,areaId_tv,areaId_tax_tv;
    private LinearLayout all_tag,portion_tag;
    private RelativeLayout price_layout;
    private String role;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expert_recorder_dataile);
        role = GetLoginInfo.getLoginInfoInstance(this).readUser().getRole();
        uuid = getIntent().getStringExtra("uuid");
        initView();
        getData();
    }

    public void initView() {
        setTitleText("录音详情");
        additional = findViewById(R.id.menu);
        title = findViewById(R.id.title);
        price = findViewById(R.id.price);
        price_layout = findViewById(R.id.price_layout);
        if(role.equals("2")|| role.equals("3")){
            price_layout.setVisibility(View.VISIBLE);
        }else{
            price_layout.setVisibility(View.GONE);
        }
        content = findViewById(R.id.content);
        trade_tv = findViewById(R.id.trade);
        tax_tv = findViewById(R.id.tax);
        topic_tv = findViewById(R.id.topic);
        areaId_tax_tv = findViewById(R.id.areaId_tax);
        areaId_tv = findViewById(R.id.areaId);
        all_tag = findViewById(R.id.all_tag);
        portion_tag = findViewById(R.id.portion_tag);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listenRecordAdapter = new ListenRecordAdapter(this);
        recyclerView.setAdapter(listenRecordAdapter);
        additional.setOnClickListener(this);
    }

    /**
     * 请求详情数据
     */
    public void getData() {
        RelateDetailModel.Body body = new RelateDetailModel.Body();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.EXPERT_RECORDER_DETAIL, body, new HttpResponse(this, ExpertRecorderDetailModel.class) {
            @Override
            public void onResult(Object obj) {
                ExpertRecorderDetailModel model = (ExpertRecorderDetailModel) obj;

                ExpertRecorderDetailModel.DataBean dataBean = model.getData();
                if (dataBean != null) {
                    ExpertRecorderDetailModel.DataBean.ExpertQuBean expertQu = dataBean.getExpertQu();
                    String status = expertQu.getStatus();
                    if(status.equals("1")||status.equals("4")){
                        additional.setVisibility(View.VISIBLE);
                        additional.setText("补录");
                    }else{
                        additional.setVisibility(View.GONE);
                    }
                    if (expertQu != null) {
                        recorder_title = expertQu.getQuestion();
                        title.setText(expertQu.getQuestion());
                        recorder_price = String.valueOf(expertQu.getQuPrice());
                        price.setText(String.valueOf(expertQu.getQuPrice()));
                        String areaId = expertQu.getAreaId();
                        String tax = expertQu.getTax();
                        String topic = expertQu.getTopic();
                        String trade = expertQu.getTrade();
                        if (!TextUtils.isEmpty(areaId)) {
                            areaId_tv.setText(areaId);
                            areaId_tax_tv.setText(tax);
                            all_tag.setVisibility(View.GONE);
                            portion_tag.setVisibility(View.VISIBLE);
                        }else {
                            if (!TextUtils.isEmpty(tax)) {
                                tax_tv.setText(tax);
                                portion_tag.setVisibility(View.GONE);
                                all_tag.setVisibility(View.VISIBLE);
                            }
                            if (!TextUtils.isEmpty(topic)) {
                                topic_tv.setText(topic);
                                portion_tag.setVisibility(View.GONE);
                                all_tag.setVisibility(View.VISIBLE);
                            }
                            if (!TextUtils.isEmpty(trade)) {
                                trade_tv.setText(trade);
                                portion_tag.setVisibility(View.GONE);
                                all_tag.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                    List<ExpertRecorderDetailModel.DataBean.ExpertVoicesBean> expertVoices = dataBean.getExpertVoices();
                    listenRecordAdapter.setData(expertVoices);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.menu:
                IntentManager.goAdditionalRecorderActivity(ExpertRecorderDataileActivity.this,uuid,recorder_title,recorder_price);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (listenRecordAdapter!=null){
            listenRecordAdapter.clear();
            listenRecordAdapter = null;
        }
    }
}
